<html>
<head>
<meta http-equiv="cache-control" content="no-cache, must-revalidate"/>
<meta http-equiv="Expires" Content="Mon, 25 May 2009 19:07:03 GMT">
<script type="text/javascript" src="../../src/gew.js"></script>

<script type="text/javascript">
var testScreenIdx = 0;
var elapseMls = 0;
var frameCount = 0;
function MyScreen(game)
{
  gewScreen.call(this, game);
  this.scene3d = new gewScene3D(this.game);
  this.onTick = function(mls)
  {
    var gl = this.game.gl;
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    this.scene3d.render();
    elapseMls += mls;
    frameCount++;
    if (elapseMls >= 2000) 
    {
      var fps = frameCount / 2;
      document.getElementById("fps").innerHTML = "Frame Rate:" + fps;
      elapseMls = 0;
      frameCount = 0;
    }
  }
  this.init = function()
  {
    var game = this.game;
    var gl = this.game.gl;
    var scene3d = this.scene3d;
    var root = scene3d.root;
    var camera_system = scene3d.camera_system;
    // init camera
    var cam = camera_system.createCamera(gewWorldCamera, null, root);
    this.camera = cam;
    this.camY = 256.;
    this.camZ = 256.;
    cam.setPositionXYZ(0.0,this.camY,0.0);
    cam.setDesiredPos(new gewVec3(0.0, this.camY, this.camZ));
    cam.setPitch(-gewCONST_VALUES.PI / 4);
    cam.setFov(60.0);
    cam.setAspectRatio(1.0);
    cam.setNear(1.);
    cam.setFar(1000);
    cam.setViewport(0, 0, game.width, game.height);
    camera_system.setActiveCamera(cam);
    var effect = new gewTerrainEffect(game);
    scene3d.addEffect(effect);
    var material = new gewTerrainMaterial(effect);
    var tmanager = this.game.textureManager;
    var m_manager = this.game.meshManager;
    var texParam = new gewTextureParameter(gl.NEAREST,gl.NEAREST,gl.MIRRORED_REPEAT,gl.MIRRORED_REPEAT);
    var texParam2 = new gewTextureParameter(gl.LINEAR,gl.LINEAR_MIPMAP_LINEAR,gl.MIRRORED_REPEAT,gl.MIRRORED_REPEAT);
    var texParam3 = new gewTextureParameter(gl.LINEAR,gl.LINEAR_MIPMAP_LINEAR,gl.MIRRORED_REPEAT,gl.MIRRORED_REPEAT)
    var texParam4 = new gewTextureParameter(gl.LINEAR,gl.LINEAR,gl.CLAMP_TO_EDGE,gl.CLAMP_TO_EDGE)
    tmanager.loadTexture("heightmap", "Resource/heightmap.png",texParam,false);
    tmanager.loadTexture("texturemap","Resource/texturemap.jpg",texParam2,true);
    tmanager.loadTexture("alphamap","Resource/test.png",texParam3,true);
    tmanager.loadTexture("materialmap","Resource/altas0.png",texParam4,false);
    material.heightMap = tmanager.getTexture("heightmap");
    material.textureMap = tmanager.getTexture("texturemap");
    material.alphaMap = tmanager.getTexture("alphamap");
    material.materialAtlas = tmanager.getTexture("materialmap");
    var knight_mesh = m_manager.getMesh("terrain");
    var terrain_node = new gewTerrainNode(gl,32,1,6);
    //terrain_node.setMesh(knight_mesh);
    terrain_node.addMaterial(material);
    terrain_node.setPositionXYZ(0,0,256);
    terrain_node.debugName = "terrain";
    root.appendChild(terrain_node);
    this.terrain = terrain_node;
    scene3d.terrain = terrain_node;
  }
  this.isDrag = false;
  this.onMouseDown = function(e)
  {
    if(e.button == MOUSE_LEFT)
    {
      this.terrain.setPosition( this.terrain.getPosition().add(new gewVec3(1,0,0)) );
    }
    else if(e.button == MOUSE_RIGHT)
    {
      this.terrain.setPosition( this.terrain.getPosition().add(new gewVec3(0,0,1)) );
    }
    return false;
  }
  this.onMouseMove = function(e)
  { 
    return false;
  }
  this.onMouseUp = function(e)
  {
    return true;
  }
  this.onMouseWheel = function(e)
  {
    if(e.delta>0)
      this.camY-=6;
    else
      this.camY+=6;
    this.camera.setPositionXYZ(this.camX, this.camY, this.camZ);
    this.camera.setDesiredPos(new gewVec3(this.camX, this.camY, this.camZ));
  }
}

function main()
{
  var game = new gewGame("cavans_id");
  var blackScreen = new MyScreen(game);
  game.addScreen(blackScreen, testScreenIdx);
  game.setScreen(testScreenIdx);
  blackScreen.init();
  setInterval(function()
  {
    game.run()
  }, 10);
}
</script>
</head>
<body onload="main()" oncontextmenu="return false">
  <div style="text-align: center">
    <!-- this is the canvas that we render to -->
    <canvas id="cavans_id" width="640" height="480"></canvas>

    <!-- a convenient div to use to display log messages -->
    <div id="fps"></div>
    <a>Dont see anything? Get FireFox 3.7 and enable webGL feature ( about:config >> set webGL.. param to true )</a>
  </div>
</body>
</html>                  